package com.hunttown.mes.common.utils;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.pool.PoolableObjectFactory;

/**
 * created by hunttown on 2017/7/27
 */
public class PoolHttpClientFactory implements PoolableObjectFactory {
    private static final int contimeout = 1000 * 20;
    private static final int sotimeout = 1000 * 20;

    @Override
    public Object makeObject() throws Exception {
        System.out.println("new HttpClient");
        HttpClient httpClient = new HttpClient();
        HttpConnectionManagerParams configParams = httpClient.getHttpConnectionManager().getParams();
        configParams.setConnectionTimeout(contimeout);
        configParams.setSoTimeout(sotimeout);
        httpClient.getParams().setConnectionManagerTimeout(contimeout);
        httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
        return httpClient;
    }

    @Override
    public void destroyObject(Object o) throws Exception {

    }

    @Override
    public boolean validateObject(Object o) {
        return false;
    }

    @Override
    public void activateObject(Object o) throws Exception {

    }

    @Override
    public void passivateObject(Object o) throws Exception {

    }
}
